﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Abp.Application.Services.Dto;
using Abp.Web.Mvc.Authorization;
using MyCompanyName.AbpZeroTemplate.Authorization;
using MyCompanyName.AbpZeroTemplate.CategoryApp;
using MyCompanyName.AbpZeroTemplate.ProductApp;
using MyCompanyName.AbpZeroTemplate.ProductApp.Dto;
using MyCompanyName.AbpZeroTemplate.Web.Areas.Mpa.Models.Product;
using MyCompanyName.AbpZeroTemplate.Web.Controllers;

namespace MyCompanyName.AbpZeroTemplate.Web.Areas.Mpa.Controllers
{
    [AbpMvcAuthorize(AppPermissions.Pages_Product)]
    public class ProductController : AbpZeroTemplateControllerBase
    {
        private ICategoryAppService _categoryAppService;
        private IProductAppService _productAppService;
        public ProductController(ICategoryAppService categoryAppService,IProductAppService productAppService)
        {
            _categoryAppService = categoryAppService;
            _productAppService = productAppService;
        }
        // GET: Mpa/Product
        public ActionResult Index()
        {
            GetCategoriesToSelect();
            return View();
        }

        [AbpMvcAuthorize(AppPermissions.Pages_Product_Create)]
        public ActionResult CreateModal()
        {
            GetCategoriesToSelect();
            return PartialView("_CreateModal");
        }

        private void GetCategoriesToSelect(object value=null)
        {
            var categories = _categoryAppService.GetCategoriesToList();
            ViewBag.categories = new SelectList(categories, "Id", "Name",value);
        }

        [AbpMvcAuthorize(AppPermissions.Pages_Product_Edit)]
        public async Task<ActionResult> EditModal(Guid id)
        {
            ProductDto product =await _productAppService.GetProductForEditAsync(new EntityRequestInput<Guid>(id));
            GetCategoriesToSelect(product.CategoryId);
            ProductViewModel productViewModel = new ProductViewModel()
            {
                Id = product.Id,
                Name = product.Name,
                Price = product.Price,
                FileName =product.FileName
            };
            return PartialView("_EditModal", productViewModel);
        }
    }
}